export default {
  name: 'newMember',
  components: {
    cityCascader: () => import("../../../component/cityCascader/index.vue")
  },
  data() {
    return {
      form: {
        startdate: '',
        enddate: '',
        sex: '',
        qualification: '',
        minage: '',
        maxage: '',
        province: "",
        city: "",
        area: "",
        pagenum: 1,
        pagesize: 10,
        citys: []
      },
      level: '0',
      sexOptions: [{
          value: 1,
          label: "男"
        },
        {
          value: 2,
          label: "女"
        },
        {
          value: 3,
          label: "未填"
        }
      ],
      educationOptions: [{
          value: 1,
          label: "初中"
        },
        {
          value: 2,
          label: "高中"
        },
        {
          value: 3,
          label: "中技"
        },
        {
          value: 4,
          label: "中专"
        },
        {
          value: 6,
          label: "本科"
        },
        {
          value: 7,
          label: "硕士"
        },
        {
          value: 8,
          label: "MBA"
        },
        {
          value: 9,
          label: "博士"
        },
        {
          value: 10,
          label: "其他"
        },
        {
          value: 11,
          label: "未填"
        }
      ],
      tableData: [],
      total: 0,
      loading: false,
    }
  },
  created() {
    this.form.startdate = this.$route.params.startdate || '';
    this.form.enddate = this.$route.params.enddate || '';
    this.getTableData();
  },
  methods: {
    // 选择城市
    handleCityChange(val) {
      this.form.citys = val;
      this.form.province = val[0];
      this.form.city = val[1];
      this.form.area = val[2];
    },
    // 时间比较
    compareDate() {
      let startdate = Date.parse(this.form.startdate);
      let enddate = Date.parse(this.form.enddate);
      if (startdate > enddate) {
        this.$message({
          message: '开始时间不能大于结束时间',
          type: 'error'
        });
        return false;
      } else {
        return true;
      }
    },
    getTableData() {
      if (!this.compareDate()) {
        return
      } else {
        if (this.form.minage && this.form.maxage && (+this.form.minage > +this.form.maxage)) {
          this.$message({
            message: "开始年龄不能大于结束年龄",
            type: 'error'
          });
          return;
        } else {
          this.loading = true
          this.$axios.$post('/app/v1/platform/summary/newMembers', this.form).then(res => {
            this.loading = false;
            if (res.data.message === 'success.') {
              this.tableData = res.data.data.list;
              this.total = parseInt(res.data.data.totalnum);
            }
          })
        }
      }
    },
    search() {
      if (!this.form.startdate && !this.form.enddate && !this.form.region) {
        this.form.startdate = '';
        this.form.enddate = '';
        this.form.region = '';
      }
      this.form.pagenum = 1;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.form.pagenum = val;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.form.pagenum = 1;
      this.form.pagesize = val;
      this.getTableData();
    },
    exportData() {
      this.$axios({
        method: 'post',
        url: '/app/v1/platform/statistic/newMemberOut',
        data: this.form,
        responseType: 'blob'
      }).then(res => {
        this.download(res)
      }).catch((error) => {
        this.$message({
          message: '导出失败',
          type: 'error'
        });
      })
    },
    download(res) {
      if (!res) {
        return
      }
      let bob = new Blob([res.data]);
      let fileName = (res.headers['content-disposition']).split('=')[1];
      fileName = decodeURI(fileName);
      let url = window.URL.createObjectURL(bob);
      let link = document.createElement('a');
      link.style.display = 'none';
      link.href = url;
      link.setAttribute('download', fileName);
      document.body.appendChild(link);
      link.click();
    },
    handleChangeInput(val) {
      var reg = /^[+]{0,1}(\d+)$/;
      if (val !== '' && !reg.test(val)) {
        this.$message({
          message: "请输入正确的年龄",
          type: "warning"
        });
        this.form.minage = "";
      }
    },
    handleChangeInput2(val) {
      var reg = /^[+]{0,1}(\d+)$/;
      if (val !== '' &&  !reg.test(val)) {
        this.$message({
          message: "请输入正确的年龄",
          type: "warning"
        });
        this.form.maxage = "";
      }
    }
  },
  filters: {
    filterSex: function (value) {
      if (value === '1') {
        return '男';
      } else if (value === '2') {
        return '女';
      }else {
        return '未填';
      }
    }
  }
}